package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.SoundApi;
import com.tanhua.dubbo.server.pojo.Sound;
import com.tanhua.server.pojo.User;
import com.tanhua.server.pojo.UserInfo;
import com.tanhua.server.utils.UserThreadLocal;
import com.tanhua.server.vo.SoundUploadResult;
import com.tanhua.server.vo.SoundVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@Service
public class SoundService {

    @Autowired
    private SoundUploadService soundUploadService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Reference(version = "1.0.0")
    private SoundApi soundApi;

    @Autowired
    private UserInfoService userInfoService;


    public String saveSound(MultipartFile soundFile) {
        User user = UserThreadLocal.get();
        Sound sound = new Sound();
        sound.setUserId(user.getId());

        //上传声音
        //调用声音上传工具类上传声音
        SoundUploadResult soundUploadResult = this.soundUploadService.upload(soundFile);
        //获取上传的路径
        String soundUrl = soundUploadResult.getName();
        //将路径保存到sound对象中
        sound.setSoundUrl(soundUrl);

        return this.soundApi.saveSound(sound);
    }

    public SoundVo receiveSound() {
        //获取UserID
        User user = UserThreadLocal.get();
        //创建sound虚拟实体类
        SoundVo soundVo = new SoundVo();
        Sound aSound = new Sound();
        try {
            //创建存入redis中的key
            String getKey = "SOUND_GET_" + user.getId();
            //获取值
            String count = this.redisTemplate.opsForValue().get(getKey);
            //判断count如果为空,则赋给其默认值
            if (StringUtils.isEmpty(count)) {
                this.redisTemplate.opsForValue().set(getKey, String.valueOf(10));
                count = "10";
            }
            //将count转为integer类型
            Integer integer = Integer.valueOf(count);
            //判断integer是否大于零
            if (integer > 0) {
                /*
                由于使用的是随机取出的API,个人无法控制取出的语音与发送者的id是否是同一个人,故在此做判断
                当语音的发送者的id不等于当前用户的id时
                */
                List<Sound> sounds = this.soundApi.receiveSound();
                //如果集合为空,则说明数据库中的声音已经被听完
                if (CollectionUtils.isEmpty(sounds)) {
                    return null;
                }
                for (Sound sound : sounds) {
                    if (!sound.getUserId().equals(user.getId())) {
                        aSound = sound;
                        boolean removeSound = this.soundApi.removeSound(aSound);
                        if (removeSound) {
                            break;
                        } else {
                            return null;
                        }
                    }
                }
            }
            //给声音赋值
            soundVo.setId((aSound.getUserId()));
            UserInfo userInfo = this.userInfoService.queryUserInfoById(aSound.getUserId());
            soundVo.setAvatar(userInfo.getLogo());
            soundVo.setNickname(userInfo.getNickName());
            soundVo.setGender(userInfo.getSex().name().toLowerCase());
            soundVo.setAge(userInfo.getAge());
            soundVo.setSoundUrl(aSound.getSoundUrl());
            //让其接收语音的次数减少
            integer--;
            //获取指定key的值进行减1
            this.redisTemplate.opsForValue().decrement(getKey);
            //将自减过的次数存入对象
            soundVo.setRemainingTimes(integer);
            return soundVo;
        } catch (Exception e) {
            return null;
        }
    }
}
